package com.lineage.server.timecontroller.npc;

import com.lineage.server.datatables.lock.SpawnBossReading;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.L1Spawn;
import com.lineage.server.thread.GeneralThreadPool;
import java.util.Iterator;
import java.util.Map;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class NpcExistTimer extends TimerTask {
  private static final Log _log = LogFactory.getLog(NpcExistTimer.class);
  
  private ScheduledFuture<?> _timer;
  
  public void start() {
    int timeMillis = 1000;
    this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, 1000L, 1000L);
  }
  
  public void run() {
    try {
      Map<Integer, L1Spawn> BossSpawn = SpawnBossReading.get().get_bossSpawnTable();
      Iterator<L1Spawn> iterator = BossSpawn.values().iterator();
      while (iterator.hasNext()) {
        L1Spawn spawn = iterator.next();
        if (spawn.get_existTime() > 0) {
          long existTime = (spawn.get_existTime() * 60 * 1000);
          long spawnTime = spawn.get_nextSpawnTime().getTimeInMillis();
          long nowTime = System.currentTimeMillis();
          L1NpcInstance npc = spawn.getNpcTemp();
          if (existTime + spawnTime < nowTime)
            npc.deleteMe(); 
        } 
        Thread.sleep(50L);
      } 
    } catch (Exception e) {
      _log.error("BOSS存在时间限制时间轴异常重启", e);
      GeneralThreadPool.get().cancel(this._timer, false);
      NpcExistTimer npcexistTimer = new NpcExistTimer();
      npcexistTimer.start();
    } 
  }
}
